<?php
require("../e/class/connect.php");
require("../e/class/db_sql.php");
require('../e/data/dbcache/class.php');
require("../e/member/class/user.php");
require("../e/data/dbcache/MemberLevel.php");
require("../e/extend/wechat/wechat.class.php");
require("../e/extend/wechat/wechat.config.php");
require("../e/extend/wechat/wechat.fun.php");
$link=db_connect();
$empire=new mysqlquery();
// 微信类
$WeiXin  = new Wechat($options);

//检测登录状态
$user=qCheckLoginAuthstr();
if(!$user['islogin']) wxErrorJson(1,'登录失败，请刷新后重试');
$user=$empire->fetch1("select userid,username,userdate,userfen from {$dbtbpre}enewsmember where username='$user[username]' limit 1");
$openid=$user['username'];

if($_GET['enews']=='Buy'){
	$arr = $WeiXin->getRev()->getRevData();
	Buy($openid,$user['userid'],$user['userfen']);
}
else if($_POST['enews']=='addStudent'){ //添加学生
	addStudent($openid,$user['userid']);
}
else if($_POST['enews']=='editStudent'){ //编辑学员
	editStudent($openid,$user['userid']);
}
else if($_POST['enews']=='deleteStudent'){ //删除学员
	deleteStudent($openid,$user['userid']);
}
else if($_POST['enews']=='chongZhiKeShi'){ //充值课时
	chongZhiKeShi($openid,$user['userid']);
}
else if($_POST['enews']=='qianDao'){ //上课签到
	qianDao($openid,$user['userid']);
}
else if($_POST['enews']=='shangKeList'){ //上课记录
	shangKeList($openid,$user['userid']);
}
else if($_POST['enews']=='shangKeList2'){ //上课记录2
	shangKeList2($openid,$user['userid']);
}
else if($_POST['enews']=='feedBack'){
	feedBack($openid,$user['userid']);
}
else if($_POST['enews']=='addNews'){
	addNews($openid,$user['userid']);
}
else if($_POST['enews']=='checkFeedBack'){
	checkFeedBack($openid,$user['userid']);
}
else if($_GET['enews']=='WXPayOk'){
	WXPayOk();
}
else if($_POST['enews']=='toMail'){
	toMail($openid,$user['userid'],$user['oicq']);
}



db_close();
$empire=null;
exit;

//添加学员
function addStudent($openid,$userid){
	global $options,$WeiXin,$empire;
	//检测教员身份
	$t = checkTeacher($openid);
	$uidName = RepPostVar($_POST['uidName']);
	$week1 = (int)$_POST['week1'];
	$week2 = (int)$_POST['week2'];
	$time1 = RepPostVar($_POST['time1']);
	$time2 = RepPostVar($_POST['time2']);
	$title = RepPostVar($_POST['title']);
	$title = $title?$title:date("Y-m-d H:i:s");
	if(!$uidName) wxErrorJson(11,'请填写学生姓名');
	if(!$week1&&!$week2) wxErrorJson(11,'请选择上课时间');
	else if(!$week1)
	{
		$week1 = $week2;
		$time1 = $time2;
	}
	$week2  = $week2&&$time2?$week2:'';
	$time2  = $week2&&$time2?$time2:'';
	$sendTime1=getWeekSend($week1).' '.$time1;
	$sendTime2=$week2?getWeekSend($week2).' '.$time2:'2038-01-19 03:14:08';
	$week1 = $week1==7?0:$week1;
	$week2 = $week2==7?0:$week2;
	
	//写数据使用记录
	$param = array();
	$param['classid']  = $options['student'];
	$param['title']    = $title;
	$param['tid']      = $t['tid'];
	$param['uidName']  = $uidName;
	$param['tidName']  = $t['tidName'];
	$param['ismember'] = 1;
	$param['userid']   = $userid;
	$param['username'] = $openid;
	$param['keMu']     = $t['keMu'];
	$param['topenid']  = $t['openid'];
	$param['week1']    = $week1;
	$param['week2']    = $week2;
	$param['time1']    = $time1;
	$param['time2']    = $time2;
	$param['sendTime1'] = $sendTime1;
	$param['sendTime2'] = $sendTime2;
	$result=ECMS_Infor_Curl($param);
	if($result['id'])
	{
		getStudentQR($result['id']);
		wxErrorJson(0,"添加成功");

		//通知管理人员
		/*
		待办事项提醒
		{{first.DATA}}
		事项名称：{{keyword1.DATA}}
		事项类型：{{keyword2.DATA}}
		通知时间：{{keyword3.DATA}}
		{{remark.DATA}}
		
		平台新增一个项目
		事项名称：项目查看
		事项类型：系统通知
		通知时间：2015年7月30日
		查看更多。
		$data = array();
		$data['touser'] = 'ooZKvsznUygTGwBak-2saCUGobFg';
		$data['template_id'] = 'DSVzXSFNma3bQ0uksJuGobYlyX4-SD2cryTUZ079C1c';
		$data['url'] = 'http://shui.dztcapp.com/webapp/?ac=feedbackList';
		$data['data']['first']['value'] = '新的反馈需要审核';
		$data['data']['keyword1']['value'] = '【'.$fl1.'-'.$fl2.'】';
		$data['data']['keyword2']['value'] = '审核信息';
		$data['data']['keyword3']['value'] = date("Y-m-d H:i");
		$data['data']['remark']['value'] = '点击进入审核';
		$res=$WeiXin->sendTemplateMessage($data);
		$data['touser'] = 'ooZKvs0C3l3ZCKlnkRqieqbt49E4';
		$res=$WeiXin->sendTemplateMessage($data);
		wxErrorJson(0,'成功',$res);
		*/
	}
	else
	{
		if(strpos($result['state'], '唯一标识') !== false)
		{
			$errMsg = '请不要重复提交';
		}
		else
		{
			$errMsg = $result['state'];
		}
		wxErrorJson(1,$errMsg);
	}
}
//编辑学员
function editStudent($openid,$userid){
	global $options,$WeiXin;
	//检测教员身份
	$t = checkTeacher($openid);
	$uidName = RepPostVar($_POST['uidName']);
	$uid = (int)$_POST['uid'];
	$week1 = (int)$_POST['week1'];
	$week2 = (int)$_POST['week2'];
	$time1 = RepPostVar($_POST['time1']);
	$time2 = RepPostVar($_POST['time2']);
	$title = RepPostVar($_POST['title']);
	$title = $title?$title:date("Y-m-d H:i:s");
	if(!$uidName) wxErrorJson(11,'请填写学生姓名');
	if(!$week1&&!$week2) wxErrorJson(11,'请选择上课时间');
	else if(!$week1)
	{
		$week1 = $week2;
		$time1 = $time2;
	}
	$week2  = $week2&&$time2?$week2:'';
	$time2  = $week2&&$time2?$time2:'';
	$sendTime1=getWeekSend($week1).' '.$time1;
	$sendTime2=$week2?getWeekSend($week2).' '.$time2:'2038-01-19 03:14:08';
	$week1 = $week1==7?0:$week1;
	$week2 = $week2==7?0:$week2;

	$param = array();
	$param['enews']    = 'EditNews';
	$param['classid']  = $options['student'];
	$param['title']    = $title;
	$param['id']       = $uid;
	$param['tid']      = $t['tid'];
	$param['tidName']  = $t['tidName'];
	$param['keMu']     = $t['keMu'];
	$param['topenid']  = $t['openid'];
	$param['week1']    = $week1;
	$param['week1']    = $week1;
	$param['week2']    = $week2;
	$param['time1']    = $time1;
	$param['time2']    = $time2;
	$param['sendTime1'] = $sendTime1;
	$param['sendTime2'] = $sendTime2;
	$param['newspath'] = date("Y-m-d");
	$param['filename'] = $uid;
	$param['uidName']  = $uidName;
	$param['ismember'] = 1;
	$param['userid']   = $userid;
	$param['username'] = $openid;
	$result=ECMS_Infor_Curl($param);
	if($result['id'])
	{
		wxErrorJson(0,"成功");
		//通知管理人员
		/*
		待办事项提醒
		{{first.DATA}}
		事项名称：{{keyword1.DATA}}
		事项类型：{{keyword2.DATA}}
		通知时间：{{keyword3.DATA}}
		{{remark.DATA}}
		
		平台新增一个项目
		事项名称：项目查看
		事项类型：系统通知
		通知时间：2015年7月30日
		查看更多。
		$data = array();
		$data['touser'] = 'ooZKvsznUygTGwBak-2saCUGobFg';
		$data['template_id'] = 'DSVzXSFNma3bQ0uksJuGobYlyX4-SD2cryTUZ079C1c';
		$data['url'] = 'http://shui.dztcapp.com/webapp/?ac=feedbackList';
		$data['data']['first']['value'] = '新的反馈需要审核';
		$data['data']['keyword1']['value'] = '【'.$fl1.'-'.$fl2.'】';
		$data['data']['keyword2']['value'] = '审核信息';
		$data['data']['keyword3']['value'] = date("Y-m-d H:i");
		$data['data']['remark']['value'] = '点击进入审核';
		$res=$WeiXin->sendTemplateMessage($data);
		$data['touser'] = 'ooZKvs0C3l3ZCKlnkRqieqbt49E4';
		$res=$WeiXin->sendTemplateMessage($data);
		wxErrorJson(0,'成功',$res);
		*/
	}
	else
	{
		if(strpos($result['state'], '唯一标识') !== false)
		{
			$errMsg = '请不要重复提交';
		}
		else
		{
			$errMsg = $result['state'];
		}
		wxErrorJson(1,$errMsg);
	}
}
//删除学员
function deleteStudent($openid,$userid){
	global $options,$WeiXin,$empire;
	//检测教员身份
	$t = checkTeacher($openid);
	$uid = (int)$_POST['uid'];
	if(!$uid) wxErrorJson(11,'请填选择学生');
	$empire->query(whereSQL($options['student'],2)."hide=1 where tid='$t[tid]' and id='$uid'");
	wxErrorJson(0,"成功",0,array(whereSQL($options['student'],2)."hide=1 where tid='$t[tid]' and id='$uid'"));
}
//充值课时
function chongZhiKeShi($openid,$userid){
	global $options,$WeiXin;
	//检测教员身份
	$t = checkTeacher($openid);
	$num = (int)$_POST['num'];
	$uid = (int)$_POST['uid'];
	$tid = $t['id'];
	$title = RepPostVar($_POST['title']);
	$title = $title?$title:date("Y-m-d H:i:s");
	$tidName = $t['tidName'];
	$uidName = RepPostVar($_POST['uidName']);
	$isonly  = RepPostVar($_POST['isonly']);
	if(!$uidName) wxErrorJson(11,'请填写学生姓名');
	else if(!$num) wxErrorJson(11,'请填写要充值的课时数量');
	
	$param = array();
	$param['classid']  = $options['log1'];
	$param['title']    = $title;
	$param['num']      = $num;
	$param['uid']      = $uid;
	$param['tid']      = $tid;
	$param['uidName']  = $uidName;
	$param['tidName']  = $tidName;
	$param['ismember'] = 1;
	$param['userid']   = $userid;
	$param['username'] = $openid;
	$param['isonly']   = $isonly;
	$result=ECMS_Infor_Curl($param);
	if($result['id'])
	{
		wxErrorJson(0,"成功");
		//通知管理人员
		/*
		待办事项提醒
		{{first.DATA}}
		事项名称：{{keyword1.DATA}}
		事项类型：{{keyword2.DATA}}
		通知时间：{{keyword3.DATA}}
		{{remark.DATA}}
		
		平台新增一个项目
		事项名称：项目查看
		事项类型：系统通知
		通知时间：2015年7月30日
		查看更多。
		$data = array();
		$data['touser'] = 'ooZKvsznUygTGwBak-2saCUGobFg';
		$data['template_id'] = 'DSVzXSFNma3bQ0uksJuGobYlyX4-SD2cryTUZ079C1c';
		$data['url'] = 'http://shui.dztcapp.com/webapp/?ac=feedbackList';
		$data['data']['first']['value'] = '新的反馈需要审核';
		$data['data']['keyword1']['value'] = '【'.$fl1.'-'.$fl2.'】';
		$data['data']['keyword2']['value'] = '审核信息';
		$data['data']['keyword3']['value'] = date("Y-m-d H:i");
		$data['data']['remark']['value'] = '点击进入审核';
		$res=$WeiXin->sendTemplateMessage($data);
		$data['touser'] = 'ooZKvs0C3l3ZCKlnkRqieqbt49E4';
		$res=$WeiXin->sendTemplateMessage($data);
		wxErrorJson(0,'成功',$res);
		*/
	}
	else
	{
		if(strpos($result['state'], '唯一标识') !== false)
		{
			$errMsg = '请不要重复提交';
		}
		else
		{
			$errMsg = $result['state'];
		}
		wxErrorJson(1,$errMsg);
	}
}
//上课签到
function qianDao($openid,$userid){
	global $options,$WeiXin;
	//检测教员身份
	$t = checkTeacher($openid);
	$uid = (int)$_POST['uid'];
	$u = checkStudent($uid);
	$tid = $t['id'];
	$num = (int)$_POST['num'];
	$title = RepPostVar($_POST['title']);
	$title = $title?$title:'无';
	$tidName = $t['tidName'];
	$uidName = RepPostVar($_POST['uidName']);
	$isonly  = RepPostVar($_POST['isonly']);
	if(!$uidName) wxErrorJson(11,'请填写学生姓名');
	
	$param = array();
	$param['classid']  = $options['log2'];
	$param['title']    = $title;
	$param['num']      = $num;
	$param['uid']      = $uid;
	$param['tid']      = $tid;
	$param['uidName']  = $uidName;
	$param['tidName']  = $tidName;
	$param['ismember'] = 1;
	$param['userid']   = $userid;
	$param['username'] = $openid;
	$param['isonly']   = $isonly;
	$result=ECMS_Infor_Curl($param);
	//echo '<pre>';
	//print_r($param);
	if($result['id'])
	{
		//通知管理人员
		/*
		wxErrorJson(0,"成功");
		待办事项提醒
		{{first.DATA}}
		事项名称：{{keyword1.DATA}}
		事项类型：{{keyword2.DATA}}
		通知时间：{{keyword3.DATA}}
		{{remark.DATA}}
		
		平台新增一个项目
		事项名称：项目查看
		事项类型：系统通知
		通知时间：2015年7月30日
		查看更多。
		*/
		$data = array();
		$data['touser'] = $u['openid'];
		$data['template_id'] = 'O8k3LSVLuPlHN8zXkTdvHR90MS1EcDa7jDwqZZhdvLo';
		$data['url'] = 'http://'.$_SERVER['HTTP_HOST'].'/webapp/?ac=jiazhang';
		$data['data']['first']['value'] = '上课签到通知';
		$data['data']['keyword1']['value'] = '张三';
		$data['data']['keyword2']['value'] = $u['keMuTxt'];
		$data['data']['keyword2']['value'] = '李老师';
		$data['data']['keyword3']['value'] = date("Y-m-d H:i");
		$data['data']['remark']['value'] = '查看更多';
		$res=$WeiXin->sendTemplateMessage($data);
		$data['touser'] = 'ommPos-kKM8qRECui0R_A4i5YW1Q';
		$res=$WeiXin->sendTemplateMessage($data);
		wxErrorJson(0,'成功',0,$res);
	}
	else
	{
		if(strpos($result['state'], '唯一标识') !== false)
		{
			$errMsg = '请不要重复提交';
		}
		else
		{
			$errMsg = $result['state'];
		}
		wxErrorJson(1,$errMsg);
	}
}
//上课记录
function shangKeList($openid,$userid){
	global $options,$empire;
	//检测教员身份
	$t = checkTeacher($openid);
	$uid = (int)$_POST['uid'];
	//角色类型
	if($_POST['tid'])
	{
		$tid = (int)$_POST['tid'];
		$where = "tid='$tid'";
	}
	else
	{
		$where = "uid='$uid'";
	}
	//充值记录
	if($_POST['type'])
	{
		$classid = $options['log1'];
		$title = "充值";
		$field = '';
	}
	else //上课记录
	{
		$classid = $options['log2'];
		$title = "上课";
		$field = '';
	}
	?>
	<div class="weui-wepay-flow-auto">
		<a href="javascript:;" class="weui-btn weui-btn_default" style="margin-bottom:20px;" id="logClose">返回</a>
		<div class="weui-wepay-flow-auto__bd">
			<div class="weui-wepay-flow-auto__li" style="height:30px;">
				<div class="weui-wepay-flow-auto__state weui-wepay-flow-auto__state_on"></div>
				<div class="weui-wepay-flow-auto__line weui-wepay-flow-auto__line_on"></div>  <!-- 竖线 -->
				<div class="weui-wepay-flow__intro-right font"><?=$title?>记录</div>
			</div>
			<?php
			$bqno=0;
			$sql=$empire->query(whereSQL($classid,0,'id,title,newstime,tidName,uidName,num'.$field).$where." order by id DESC limit 200");
			while($r=$empire->fetch($sql))
			{
				++$bqno;
				?>
			<div class="weui-wepay-flow-auto__li">
				<div class="weui-wepay-flow-auto__state weui-wepay-flow-auto__state_on"></div>
				<div class="weui-wepay-flow-auto__line weui-wepay-flow-auto__line_on"></div>  <!-- 竖线 -->
				<p class="weui-wepay-flow-auto__title"><?=date("Y-m-d H:i:s",$r['newstime'])?> <small>星期<?=week(date("w",$r['newstime']))?></small></p>
				<p class="weui-wepay-flow-auto__info">教师姓名：<?=$r['tidName']?></p>
				<p class="weui-wepay-flow-auto__info">学员姓名：<?=$r['uidName']?></p>
				<?php if($_POST['type']){?><p class="weui-wepay-flow-auto__info" style="color:#FF6633;">充值数量：<?=$r['num']?>节</p><?php } else { ?>
				<p class="weui-wepay-flow-auto__info" style="color:#FF6633;">上课情况：<?=$r['num']?>星</p><?php }?>
				<p class="weui-wepay-flow-auto__info">备注说明：<?=esub(stripslashes($r[title]),100)?></p>
			</div>
				<?php
			}
			if(!$bqno)
			{
				echo '<div class="weui-loadmore weui-loadmore_line"><span class="weui-loadmore__tips">没有信息</span></div>';
			}
			?>
			<!-- div class="weui-wepay-flow-auto__li">
				<div class="weui-wepay-flow-auto__state weui-wepay-flow-auto__state_on"></div>
				<p class="weui-wepay-flow-auto__title">&nbsp;</p>
				<p class="weui-wepay-flow-auto__info">&nbsp;</p>
			</div -->
		</div>
	</div>					
	<?php
	return;
}
//上课记录2
function shangKeList2($openid,$userid){
	global $options,$empire;
	//检测教员身份
	//$t = checkTeacher($openid);
	$uid = (int)$_POST['uid'];
	$page = (int)$_POST['page'];
	//角色类型
	if($_POST['tid'])
	{
		$tid = (int)$_POST['tid'];
		$where = "tid='$tid'";
	}
	else
	{
		$where = "uid='$uid'";
	}
	//充值记录
	if($_POST['type'])
	{
		$classid = $options['log1'];
		$title = "充值";
		$field = '';
	}
	else //上课记录
	{
		$classid = $options['log2'];
		$title = "上课";
		$field = '';
	}
	$week = array('日','一','二','三','四','五','六');
	$bqno = $page*5;
	$sql=$empire->query(whereSQL($classid,0,'id,title,newstime,tidName,uidName,num'.$field).$where." order by id DESC limit ".($page*5).",10");
	while($r=$empire->fetch($sql))
	{
		++$bqno;
		?>
	<div class="weui-cell" style="padding:5px 15px;">
		<div class="weui-cell__bd">
			<span style="vertical-align: middle"><small style="font-size: 12px;"><?=$bqno?>. <?=date("Y-m-d H:i",$r['newstime'])?></small> <small style="font-size: 12px;color: #888888;">星期<?=week(date("w",$r['newstime']))?> | <?=$r['tidName']?></small></span>
	</div>
		<div class="weui-cell__ft"><?=$r['num']?>星</div>
	</div>
		<?php
	}
	return;
}




















/* 
*function：计算两个日期相隔多少年，多少月，多少天 
*param string $date1[格式如：2011-11-5] 
*param string $date2[格式如：2012-12-01] 
*param int    0返回array 1返回多少年(m大于6等于一年)
*return array|string array('年','月','日')|1; 
*/  
function diffDate($date1,$date2,$type=0){
	if(!$date1) return 0;

    if(strtotime($date1)>strtotime($date2)){  
        $tmp=$date2;  
        $date2=$date1;  
        $date1=$tmp;  
    }  
    list($Y1,$m1,$d1)=explode('-',$date1);  
    list($Y2,$m2,$d2)=explode('-',$date2);  
    $Y=$Y2-$Y1;  
    $m=$m2-$m1;  
    $d=$d2-$d1;  
    if($d<0){  
        $d+=(int)date('t',strtotime("-1 month $date2"));  
        $m--;  
    }  
    if($m<0){  
        $m+=12;  
        $Y--;  
    }
	if($type==1)
	{
		$_Y=$m>6?1:0;
		return $Y+$_Y;
	}
 	else if($type==2)
	{
		//print_r(array('year'=>$Y,'month'=>$m,'day'=>$d));
		return ($Y*12)+$m;//($d?1:0);
	}
   return array('year'=>$Y,'month'=>$m,'day'=>$d);  
} 
/**
 * 求两个日期之间相差的天数
 * (针对1970年1月1日之后，求之前可以采用泰勒公式)
 * @param string $day1
 * @param string $day2
 * @return number
 */
function diffDays ($day1, $day2){
	$second1 = strtotime($day1);
	$second2 = strtotime($day2);
	if(date("n",$second2)<=6)
	{
		$Y=date("Y",$second2)-1;
		$second2 = strtotime($Y."-12-31");
	}
	if ($second1 < $second2) {
		$tmp = $second2;
		$second2 = $second1;
		$second1 = $tmp;
	}
	return ($second1 - $second2) / 86400;
}
function shuiLv($zengzhiMoney,$chengBen,$SL){
	if(!$zengzhiMoney||!$chengBen) return 0;
	$shuiLv = $zengzhiMoney/$chengBen*100;
	if($shuiLv<=50)
	{
		$arr = $SL[0];
		$arr[4]='一级';
	}
	else if($shuiLv>50&&$shuiLv<=100)
	{
		$arr = $SL[1];
		$arr[4]='二级';
	}
	else if($shuiLv>100&&$shuiLv<=200)
	{
		$arr = $SL[2];
		$arr[4]='三级';
	}
	else if($shuiLv>200)
	{
		$arr = $SL[3];
		$arr[4]='四级';
	}
	$arr[3]=$shuiLv;
    return $arr;  
}
function shuiLvGeRen($buyFang,$mianJi,$SD){
	//二套房
	if($buyFang==3)
	{
		$shuiLv = $SD['qishui4'];
	}
	else if($buyFang==4)
	{
		$shuiLv = $SD['qishui5'];
	}
	else
	{
		//90㎡以下含90
		if($mianJi<=90)
		{
			$shuiLv = $SD['qishui1'];	
		}
		else
		{
			//买方唯一住房
			if($buyFang==1)
			{
				$shuiLv = $SD['qishui2'];
			}
			else //买方有一套房
			{
				$shuiLv = $SD['qishui3'];
			}
		}
	}
	return $shuiLv;
}
//检测是VIP和免费用户使用次数
function checkFreeNum($ecms,$user,$r){
	global $class_r,$dbtbpre,$empire;
	$logDate = date("Y-m-d");
	if($user['vip']==8) //付费
	{}
	else //免费
	{
		if(!$r['FreeNum']) //必须包月
		{
			wxErrorJson(40004,'请包月后才能使用');
		}
		//查询今日使用次数
		$logNum  = $empire->gettotal("select count(*) as total from {$dbtbpre}ecms_".$class_r[15]['tbname']." where add_classid='$r[classid]' and openid='$user[username]' and logDate='$logDate'");
		if($logNum>=$r['FreeNum'])
		{
			wxErrorJson(40004,'5元包月即可每日无限次使用本计算服务'); //每日免费'.$r['FreeNum'].'次数用完
		}
		else
		{
			$num = $r['FreeNum']-$logNum;
			
		}
	}

	//写数据使用记录
	$param = array();
	$param['classid']     = 15;
	$param['title']       = $r['classid'].'-'.$r['id'].'-'.$user['username'];
	$param['add_infoid']  = $r['id'];
	$param['add_classid'] = $r['classid'];
	$param['uid']         = $user['userid'];
	$param['openid']      = $user['username'];
	$param['logDate']     = $logDate;
	$param['ecms']        = $ecms;
	$result=ECMS_Infor_Curl($param);
	return wxErrorJson(0,'免费使用',array('num'=>$num),1); //每日免费'.$r['FreeNum'].'次数用完
}
//
function Buy($openid,$userid,$userfen){
	global $class_r,$dbtbpre,$empire,$WeiXin;
	$id      = (int)$_GET['id'];
	$no      = (int)$_GET['no']+1;
	$time    = time();
	$classid = 18;
	$tbname  = $class_r[$classid]['tbname'];
	$F = 'price'.$no;
	$r=$empire->fetch1("select id,fen,".$F." from {$dbtbpre}ecms_$tbname where classid='$classid' and id='$id' limit 1");
	if(!$r['id'])
	{
		wxErrorJson(1,'支付ID错误');
	}
	$price = $r[$F]+0;
	$price = $price?$price*100:'1';
	
	//积分抵现金
	$buyfen = 0;
	if($userfen)
	{
		$userMoney = floor($userfen/$r['fen']*100);
		$_price = $price - $userMoney;
		//最少需要支付1分钱
		if($_price>0)
		{
			$buyfen = $userMoney*$r['fen'];
			$price = $_price;
		}
		else
		{
			$buyfen = $price*$r['fen'];
			$price = 1;
		}
	}
	
	//写数据使用记录
	$param = array();
	$param['classid']  = 17;
	$param['title']    = '支付';
	$param['buyType']  = $no;
	$param['price']    = $price/100;
	$param['buyfen']   = $buyfen;
	$param['fenBiLi']  = $r['fen'];
	$param['openid']   = $openid;
	$param['newstime'] = $time;
	$param['ismember'] = 1;
	$param['username'] = $openid;
	$param['userid']   = $userid;
	$result=ECMS_Infor_Curl($param);
	if($result['id'])
	{
		//if($openid=='ooZKvs0C3l3ZCKlnkRqieqbt49E4') $price = 1;
		$days=array(30,60,90,180,360,720);
		$arr=array();
		$arr['spbill_create_ip'] = egetip();
		$arr['out_trade_no'] = $result['id'].'|'.$time; //商户订单号
		$arr['total_fee']    = $price;                  //标价金额
		$arr['notify_url']   = "http://".$_SERVER['HTTP_HOST']."/e/extend/wechat/wechat.PayNotify.php";
		$arr['body']         = $F.'|包月费用';          //商品描述
		//记录栏目ID|记录信息ID|交易时间|购买天数|分数数量
		$arr['attach']       = '17|'.$result['id'].'|'.$time.'|'.$days[$no-1].'|'.$buyfen;     //附加数据
		$arr['openid']       = $openid;                 //用户标识
	
		$ret = $WeiXin->PayUnifiedOrder($arr,true);
		if($ret)
		{
			$ret['id']=$result['id'];
			$ret['id']=$result['id'];
			$ret['id']=$result['id'];
			$ret['id']=$result['id'];
			wxErrorJson(0,'成功',$ret);
			//$arr = array('errCode'=>0,'errMsg'=>"成功",'ret'=>$ret);
		}
		else
		{
			wxErrorJson($WeiXin->errCode,$WeiXin->errMsg);
			//$arr = array('errCode'=>$WeiXin->errCode,'errMsg'=>$WeiXin->errMsg);
		}
		//echo json_encode($arr);
	}
	else
	{
		wxErrorJson(1,'订单信息错误');
	}
}
function WXPayOk(){
	global $class_r,$dbtbpre,$empire;
	$id      = (int)$_GET['id'];
	$classid = 17;
	$tbname  = $class_r[$classid]['tbname'];
	$r=$empire->fetch1("select id,haveprice from {$dbtbpre}ecms_$tbname where classid='$classid' and id='$id' limit 1");
	if(!$r['id'])
	{
		wxErrorJson(1,'支付ID错误');
	}
	else if($r['haveprice']==2)
	{
		wxErrorJson(0,'已确认');
	}
	$empire->query("update {$dbtbpre}ecms_".$tbname." set haveprice=1 where id='$id' limit 1");
	wxErrorJson(0,'已支付');
}
function feedBack($openid,$userid){
	global $class_r,$dbtbpre,$empire,$WeiXin;
	//$prov = RepPostVar($_POST['prov']);
	//$city = RepPostVar($_POST['city']);
	//$dist = RepPostVar($_POST['dist']);
	$c = RepPostVar($_POST['c']);
	$c = explode($c);
	$prov = $c[0];
	$city = $c[1];
	$dist = $c[2];
	$fl1  = RepPostVar($_POST['fl1']);
	$fl2  = RepPostVar($_POST['fl2']);
	$title= RepPostVar($_POST['title']);
	if(!$title) wxErrorJson(11,'请填写问题说明');
	
	//{"p":"住宅","c":[{"n":"增值税"},{"n":"城建税"},{"n":""},{"n":""},{"n":""},{"n":""},{"n":""},{"n":""},{"n":""}]},
	//{"p":"非住宅","c":[{"n":"增值税"},{"n":""},{"n":""},{"n":""},{"n":""},{"n":""},{"n":""},{"n":""},{"n":""},{"n":""},{"n":""},{"n":""},{"n":""}]},
	//{"p":"房产税","c":[{"n":""},{"n":""},{"n":""}]},

	
	if($fl1=='住宅')
	{
		$array=array('增值税','城建税','教育费附加','地方教育附加','个人所得税','契税','登记费','权籍调查','税费共计');
		$F='z';
		$key = array_search($fl2, $array)+1;
		$F=$F.$key;
	}
	else if($fl1=='非住宅')
	{
		$arr=array('增值税','城建税','教育费附加','地方教育附加','个人所得税','契税','土地增值税','卖方印花税','买方印花税','登记费','权籍调查','交易手续','税费共计');
		$F='f';
		$key = array_search($fl2, $array)+1;
		$F=$F.$key;
	}
	else if($fl1=='房产税')
	{
		$arr=array('应缴税','滞纳金','总计金额');
		$F='s';
		$key = array_search($fl2, $array)+1;
		$F=$F.$key;
	}
	$classid = 18;
	$tbname  = $class_r[$classid]['tbname'];
	$r=$empire->fetch1("select id,".$F." from {$dbtbpre}ecms_$tbname where classid='$classid' limit 1");
	$fen = (int)$r[$F];
	//写数据使用记录
	$classid = 21;
	$param = array();
	$param['classid'] = $classid;
	$param['title']   = $title;
	$param['prov']    = $prov;
	$param['city']    = $city;
	$param['dist']    = $dist;
	$param['fl1']     = $fl1;
	$param['fl2']     = $fl2;
	$param['fen']     = $fen;
	$param['ismember'] = 1;
	$param['username'] = $openid;
	$param['userid']   = $userid;
	$param['openid']   = $openid;
	$param['newstime'] = $time;
	$param['isonly']   = $openid.'#'.$prov.'#'.$city.'#'.$dist.'#'.$fl1.'#'.$fl2;
	$result=ECMS_Infor_Curl($param);
	if($result['id'])
	{
		//通知管理人员
		/*
		待办事项提醒
		{{first.DATA}}
		事项名称：{{keyword1.DATA}}
		事项类型：{{keyword2.DATA}}
		通知时间：{{keyword3.DATA}}
		{{remark.DATA}}
		
		平台新增一个项目
		事项名称：项目查看
		事项类型：系统通知
		通知时间：2015年7月30日
		查看更多。
		*/
		$data = array();
		$data['touser'] = 'ooZKvsznUygTGwBak-2saCUGobFg';
		$data['template_id'] = 'DSVzXSFNma3bQ0uksJuGobYlyX4-SD2cryTUZ079C1c';
		$data['url'] = 'http://shui.dztcapp.com/webapp/?ac=feedbackList';
		$data['data']['first']['value'] = '新的反馈需要审核';
		$data['data']['keyword1']['value'] = '【'.$fl1.'-'.$fl2.'】';
		$data['data']['keyword2']['value'] = '审核信息';
		$data['data']['keyword3']['value'] = date("Y-m-d H:i");
		$data['data']['remark']['value'] = '点击进入审核';
		$res=$WeiXin->sendTemplateMessage($data);
		$data['touser'] = 'ooZKvs0C3l3ZCKlnkRqieqbt49E4';
		$res=$WeiXin->sendTemplateMessage($data);
		wxErrorJson(0,'成功',$res);
	}
	else
	{
		if(strpos($result['state'], '唯一标识') !== false)
		{
			$errMsg = '请不要重复提交';
		}
		else
		{
			$errMsg = $result['state'];
		}
		wxErrorJson(1,$errMsg);
	}
}
function checkFeedBack($openid,$userid){
	global $class_r,$dbtbpre,$empire,$WeiXin;
	if($_POST['checkType']=='YES')
	{
		$checkType=2;
		$checkText='【通过】';
	}
	else
	{
		$checkType=1;
		$checkText='【不通过】';
	}
	$id   = (int)$_POST['inforID'];
	$classid = 21;
	$tbname  = $class_r[$classid]['tbname'];
	$r=$empire->fetch1("select id,checkType,fen,openid,newstime from {$dbtbpre}ecms_$tbname where classid='$classid' and id='$id' limit 1");
	if(!$r['id'])
	{
		wxErrorJson(11,'信息ID错误');
	}
	else if($r['checkType'])
	{
		wxErrorJson(11,'已经审核过，不能重审核');
	}
	$empire->query("update {$dbtbpre}ecms_$tbname set checkType='$checkType' where id='$id'"); 
	$addFen=(int)$r['fen'];
	$empire->query("update {$dbtbpre}enewsmember set userfen=userfen+'$addFen' where userid='$userid'");

	//通知会员
	/*
	反馈结果通知
	{{first.DATA}}
	反馈时间：{{keyword1.DATA}}
	回复时间：{{keyword2.DATA}}
	{{remark.DATA}}
	
	亲爱的业主，您的意见反馈已回复
	反馈时间：2017/6/21 10:20:46
	回复时间：2017/6/21 10:20:46
	点击详情查看回复信息
	*/
	$data = array();
	$data['touser'] = $r['openid'];
	$data['template_id'] = 'cqDRCjGzchGqc19ACBteJL2STliIQocBMovm-yxNwUA';
	$data['url'] = 'http://shui.dztcapp.com/webapp/?ac=feedback';
	$data['data']['first']['value'] = '审核结果通知'.$checkText;
	$data['data']['keyword1']['value'] = date("Y-m-d H:i",$r['newstime']);
	$data['data']['keyword2']['value'] = date("Y-m-d H:i");
	$data['data']['remark']['value'] = '点击查看详情';
	$res=$WeiXin->sendTemplateMessage($data);
	$data['touser'] = 'ooZKvs0C3l3ZCKlnkRqieqbt49E4';
	$res=$WeiXin->sendTemplateMessage($data);
	wxErrorJson(0,'成功',$res);
}
function toMail($openid,$userid){
	global $class_r,$dbtbpre,$empire;
	$user=$empire->fetch1("select oicq from {$dbtbpre}enewsmemberadd where userid='$userid' limit 1");

	$c=RepPostVar($_POST['c']);
	$file=RepPostVarWx($_POST['file']);
	//$file=$_POST['file'];
	if(!$file) wxErrorJson(1,'附件地址不能为空');
	$email=RepPostVar($_POST['email']);
	if(!$email) wxErrorJson(1,'邮箱地址不能为空');
	//$id=(int)$_POST['id'];
	$no=(int)$_POST['no'];
	$no=$no>4?0:$no;
	$Farr=array('baochui','fangchan','diya','guotu','quanji');
	$Tarr=array('报税流程','房产过户流程','抵押过户流程','国土过房流程','权藉调查流程');
	/*$F=$Farr[$no].'1';
	//获取附件地址
	$classid = 12;
	$tbname  = $class_r[$classid]['tbname'];
	$r=$empire->fetch1("select id,title,".$F." from {$dbtbpre}ecms_$tbname where classid='$classid' and id='$id' limit 1");
	if(!$r['id'])
	{
		wxErrorJson(11,'信息ID错误');
	}*/
	//更新用户邮箱地址
	if($email!=$user['oicq'])
	{
		$empire->query("update {$dbtbpre}enewsmemberadd set oicq='$email' where userid='$userid'");
	}
	
	//require_once('PHPMailer/class.phpmailer.php');
	//require_once("PHPMailer/class.smtp.php");
	$Subject = $c.'['.$Tarr[$no].']';
	require_once("../e/extend/PHPMailer/PHPMailerAutoload.php");
	$mail  = new PHPMailer(); 
	$mail->CharSet    ="UTF-8";                 //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置为 UTF-8
	$mail->IsSMTP();                            // 设定使用SMTP服务
	$mail->SMTPAuth   = true;                   // 启用 SMTP 验证功能
	$mail->SMTPSecure = "ssl";                  // SMTP 安全协议
	$mail->Host       = "smtp.163.com";         // SMTP 服务器
	$mail->Port       = 465;                    // SMTP服务器的端口号
	$mail->Username   = "pkkgu";                // SMTP服务器用户名
	$mail->Password   = "pkkgu7788";            // SMTP服务器密码
	$mail->SetFrom('pkkgu@163.com', '税费计算器');      // 设置发件人地址和名称
	$mail->AddReplyTo("pkkgu@163.com","税费计算器");    // 设置邮件回复人地址和名称
	$mail->Subject    = $Subject;                // 设置邮件标题
	$mail->AltBody    = "为了查看该邮件，请切换到支持 HTML 的邮件客户端";  // 可选项，向下兼容考虑
	$mail->MsgHTML("文件地区：".$c."文件名称：".$Subject."<br>发送时间：".date("Y-m-d H:i:s"));                         // 设置邮件内容
	$mail->AddAddress($email, "税费计算器用户(".$userid.")");      //收件人地址、名称
	$mail->AddAttachment(ECMS_PATH.$file,$Tarr[$no].'.'.pathinfo($file, PATHINFO_EXTENSION));//("PHPMailer/examples/images/phpmailer.png"); // 附件 
	if(!$mail->Send()) {
		//echo "发送失败：" . $mail->ErrorInfo;
		wxErrorJson(444,'发送失败'.$mail->ErrorInfo);
	} else {
		//echo "恭喜，邮件发送成功！";
		wxErrorJson(0,'邮件发送成功');
	}
}
function addNews($openid,$userid){
	global $class_r,$dbtbpre,$empire;
	$ftitle = RepPostVarWx($_POST['ftitle']);
	$dwz = RepPostVar($_POST['dwz']);
	$title = RepPostVar($_POST['title']);
	$newsfl = RepPostVar($_POST['newsfl']);
	$titlepic = RepPostVarWx($_POST['titlepic']);
	if(!$title) wxErrorJson(11,'请填写标题');
	else if(!$dwz) wxErrorJson(12,'请填短网址');
	else if(!$ftitle) wxErrorJson(13,'请填写链接');
	else if(!$titlepic) wxErrorJson(14,'请填写标题图片');
	if($openid=='ooZKvs0C3l3ZCKlnkRqieqbt49E4'||$openid=='ooZKvsznUygTGwBak-2saCUGobFg'||$openid=='ooZKvs0eT06K9_dpU0qnOVqRqhHI'||$openid=='ooZKvsxykbEBkuUtf8wCqUw4sR3g') 
	{}
	else
	{
		wxErrorJson(16,'权限不够');
	}
	//写数据使用记录
	$classid = 13;
	$param = array();
	$param['classid'] = $classid;
	$param['title']   = $title;
	$param['dwz']     = $prov;
	$param['ftitle']  = $ftitle;
	$param['newsfl']  = $newsfl;
	$param['titlepic'] = $titlepic;
	$param['ismember'] = 1;
	$param['username'] = $openid;
	$param['userid']   = $userid;
	$param['openid']   = $openid;
	$param['newstime'] = time();
	$result=ECMS_Infor_Curl($param);
	if($result['id'])
	{
		wxErrorJson(0,'成功',$res);
	}
	else
	{
		if(strpos($result['state'], '唯一标识') !== false)
		{
			$errMsg = '请不要重复提交';
		}
		else
		{
			$errMsg = $result['state'];
		}
		wxErrorJson(15,$errMsg);
	}
}
